package com.jsb.laundry.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsb.laundry.common.constant.CommonConstants;
import com.jsb.laundry.model.dto.posts.PostUserFansQueryRequest;
import com.jsb.laundry.model.dto.posts.PostUserFollowQueryRequest;
import com.jsb.laundry.model.entity.UserDetail;
import com.jsb.laundry.service.UserDetailService;
import com.jsb.laundry.dao.UserDetailMapper;
import com.jsb.laundry.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* @author ching
* @description 针对表【user_detail(用户端个人详情表)】的数据库操作Service实现
* @createDate 2023-09-04 15:18:50
*/
@Service
public class UserDetailServiceImpl extends ServiceImpl<UserDetailMapper, UserDetail>
    implements UserDetailService{

    @Override
    public QueryWrapper<UserDetail> getFollowQueryWrapper(PostUserFollowQueryRequest postUserFollowQueryRequest) {
        QueryWrapper<UserDetail> queryWrapper = new QueryWrapper<>();
        if (postUserFollowQueryRequest == null) {
            return queryWrapper;
        }
        String sortField = postUserFollowQueryRequest.getSortField();
        String sortOrder = postUserFollowQueryRequest.getSortOrder();
        String searchText = postUserFollowQueryRequest.getSearchText();
        // 拼接查询条件
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.like("user_name", searchText);
        }
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstants.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }

    @Override
    public QueryWrapper<UserDetail> getFansQueryWrapper(PostUserFansQueryRequest postUserFansQueryRequest) {
        QueryWrapper<UserDetail> queryWrapper = new QueryWrapper<>();
        if (postUserFansQueryRequest == null) {
            return queryWrapper;
        }
        String sortField = postUserFansQueryRequest.getSortField();
        String sortOrder = postUserFansQueryRequest.getSortOrder();
        String searchText = postUserFansQueryRequest.getSearchText();
        // 拼接查询条件
        if (StringUtils.isNotBlank(searchText)) {
            queryWrapper.like("user_name", searchText);
        }
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstants.SORT_ORDER_ASC), sortField);
        return queryWrapper;
    }

    @Override
    public Page<UserDetail> listUserFollowByPage(IPage<UserDetail> page, Wrapper<UserDetail> queryWrapper, String userId) {
        if (StringUtils.isAnyBlank(userId)) {
            return new Page<>();
        }
        return baseMapper.listUserFollowByPage(page, queryWrapper, userId);
    }

    @Override
    public Page<UserDetail> listUserFansByPage(IPage<UserDetail> page, Wrapper<UserDetail> queryWrapper, String userId) {
        if (StringUtils.isAnyBlank(userId)) {
            return new Page<>();
        }
        return baseMapper.listUserFansByPage(page, queryWrapper, userId);
    }
}




